package com.smile.spring.order;

import org.springframework.core.OrderComparator;
import org.springframework.core.annotation.AnnotationAwareOrderComparator;

import java.util.ArrayList;
import java.util.List;

/**
 * @author LiYuhang
 * @version 0.1
 * @application
 * @Copyright 上海申通快递总部
 * @company STO
 * @Date 2021/12/12 19:36
 */
public class Test {
    public static void main(String[] args) {
       // testOrderComparator();
        testAnnotationAwareOrderComparator();

    }

    private static void testAnnotationAwareOrderComparator() {
        C c = new C();
        D d = new D();

        AnnotationAwareOrderComparator comparator = new AnnotationAwareOrderComparator();
        System.out.println(comparator.compare(c, d)); // - 1

        List list = new ArrayList<>();
        list.add(new A());
        list.add(new B());
        list.add(c);
        list.add(d);

        // 按order值升序排序
        list.sort(comparator);

        System.out.println(list); // B，A
    }

    private static void testOrderComparator() {

        A a = new A(); // order=3
        B b = new B(); // order=2

        OrderComparator comparator = new OrderComparator();
        System.out.println(comparator.compare(a, b));  // 1

        List list = new ArrayList<>();
        list.add(a);
        list.add(b);

        // 按order值升序排序
        list.sort(comparator);

        System.out.println(list);  // B，A
    }
}
